﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using ZLCMS.Business.UploadFileInfo;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Common
{
    // 用于后台富文本编辑框上传图片
    public partial class ContentUpload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String err = "";
            String msg = "";
            //string extString = ".thumb.jpg";

            try
            {
                UploadHandle handle = new UploadHandle(this);

                UploadConfig Config = new UploadConfig() { UploadDir = "xheditor", Thumbnail = new ThumbnailConfig() { Auto = false, Mode = "W", Width = 800, Height = 1000, Name = "thumb" } };

                handle.SaveUploadFile(Config);

                string file = handle.File.FilePath;

                string imgExt = "jpg,jpeg,gif,png,bmp";
                string ext = GetFileExt(file);

                if (imgExt.IndexOf(ext) != -1)
                {
                    //如果为图片，则使用缩略图
                    string extString = ".thumb." + file.Substring(file.LastIndexOf('.') + 1).ToLower();
                    msg = "{'url':'" + handle.FileUrl + extString + "','localname':'" + jsonString(handle.LocalFilePath) + "','id':'1'}";
                }
                else
                {
                    msg = "{'url':'" + handle.FileUrl + "','localname':'" + jsonString(handle.LocalFilePath) + "','id':'1'}";
                }

            }
            catch (System.Exception ex)
            {
                err = ex.Message;
            }

            Response.Charset = "UTF-8";
            Response.Write("{'err':'" + jsonString(err) + "','msg':" + msg + "}");
            Response.End();
        }


        string jsonString(string str)
        {
            str = str.Replace("\\", "\\\\");
            str = str.Replace("/", "\\/");
            str = str.Replace("'", "\\'");
            return str;
        }

        protected string GetFileExt(string file)
        {
            if (file != "") return file.Substring(file.LastIndexOf('.') + 1).ToLower();
            else return "";
        }
    }
}
